#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Project  : ecshop
# @File     : page_backend_add_discount.py
# @Author   : WanLiWei
# @Time     : 2021/7/3 14:34
from time import sleep

from selenium.webdriver.common.by import By


class PageBackendAddDiscount():
    def __init__(self,driver):
        self.driver=driver
    def test_click_promotion(self):
        # 进入id为menu-frame的frame
        self.driver.switch_to.frame('menu-frame')
        # 点击促销管理
        self.driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[2]').click()

    def test_click_discount(self):
        # 点击优惠活动
        self.driver.find_element_by_xpath('//ul[@id="menu-ul"]/li[2]/ul/li[8]/a').click()
        # 退出frame
        self.driver.switch_to.default_content()
    def test_click_discountpage(self):
        # 进入id为main-frame的frame
        self.driver.switch_to.frame('main-frame')

        # 点击优惠活动
        self.driver.find_element_by_xpath('/html/body/h1/span[1]/a').click()
    def test_discount_name(self):
        # 优惠活动名称
        self.driver.find_element(By.ID, 'act_name').send_keys("双11啦lalala")
    def test_start_data(self):
        # 优惠开始时间
        # 删除元素中属性的方法--日期处理
        # 开始日期
        js = "document.getElementById('start_time').removeAttribute('ReadOnly')"
        self.driver.execute_script(js)
        self.driver.find_element_by_id("start_time").clear()
        self.driver.find_element_by_id("start_time").send_keys("2021-11-11")
    def test_end_data(self):
        # 优惠结束时间
        js = "document.getElementById('end_time').removeAttribute('ReadOnly')"
        self.driver.execute_script(js)
        self.driver.find_element_by_id("end_time").clear()
        self.driver.find_element_by_id("end_time").send_keys("2021-11-22")
    def test_click_grade(self):
        # 享受优惠的会员等级
        self.driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[4]/td[2]/input[2]').click()
    def test_discount_range(self):
        # 优惠范围
        self.driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[5]/td[2]/select/option[1]').click()
    def test_down_money(self):
        # 金额下限
        self.driver.find_element(By.ID, 'min_amount').clear()
        self.driver.find_element(By.ID, 'min_amount').send_keys(100)
    def test_up_money(self):
        # 金额上限
        self.driver.find_element(By.ID, 'max_amount').clear()
        self.driver.find_element(By.ID, 'max_amount').send_keys(1000)
    def test_discount_type(self):
        # 优惠方式
        sleep(2)
        self.driver.find_element_by_xpath('//select[@id="act_type"]/option[2]').click()
        self.driver.find_element_by_xpath('//input[@id="act_type_ext"]').clear()
        self.driver.find_element_by_xpath('//input[@id="act_type_ext"]').send_keys(50)
        sleep(3)
    def test_confirm(self):
        # 确定按钮
        self.driver.find_element_by_xpath('/html/body/div[1]/form/table/tbody/tr[11]/td/input[1]').click()
        # 退出frame
        self.driver.switch_to.default_content()
    def test_add_diecount(self):
        self.test_click_promotion()
        self.test_click_discount()
        self.test_click_discountpage()
        self.test_discount_name()
        self.test_start_data()
        self.test_end_data()
        self.test_click_grade()
        self.test_discount_range()
        self.test_down_money()
        self.test_up_money()
        self.test_discount_type()
        self.test_confirm()